import UIKit
//is as

class MediaItem {
    var name: String
    init(name: String) {
        self.name = name
    }
}

class Movie: MediaItem {
    var director: String
    init(director: String, name:String) {
        self.director = director
        super.init(name: name)
    }
}

class Song: MediaItem {
    var artist: String
    init(artist: String, name:String) {
        self.artist = artist
        super.init(name: name)
    }
}


let library = [
    Movie(director: "Micheal cuze", name: "Caslbanca"),
    Song(artist: "Evis Presley", name: "Blue Sude Shoes"),
    Movie(director: "Orson Wells1", name: "Citizen kanen"),
    Song(artist: "Chesney Hawkes", name: "The One and Only"),
    Song(artist: "Rick Astley", name: "Never Gonna")
]

//检查类型
var movieItem = 0
var songCount = 0
for item in library {
    if item is Movie {
        movieItem += 1
    } else if item is Song {
        songCount += 1
    }
}
print("movie is \(movieItem), song is \(songCount)")
//向下转型 as as? as !
for item in library {
    if let movie = item as?  Movie {
        print("Movie:\(movie.name), dir :\(movie.director)")
    } else if let song = item as? Song {
        print("song:\(song.name), artist: \(song.artist)")
    }
}
    
    
//Any AnyObject 任何class类型的实例

let someObjects:[AnyObject] =
[
    Movie(director: "Stanley Kubrick", name: "2001:A Space oDYSSEY"),
    Movie(director: "Duncan Jonea", name: "Moon"),
    Movie(director: "Ridley Soctt", name: "Aliece")
]

for obj in someObjects {
    if  let movie = obj as? Movie {
        print("Movie:\(movie.name), dir :\(movie.director)")}
}

//Any 类型 任意类型
var things = [Any]()
things.append(0)
things.append(0.0)
things.append(42)
things.append(3.14121)
things.append("hello")
things.append((3.0,4.0))
things.append(Movie(director: "dd", name: "daf"))

for thing in things {
    switch thing {
    case 0 as Int:
        print("zero is int")
    case 0.0 as Double:
        print("zero as Double")
    case let someint as Int:
        print("an integer value of \(someint)")
    case let someDouble as Double where someDouble > 0:
        print("a possive double value:\(someDouble)")
    case is Double:
        print("some other double i dont want to print")
    case let (x, y) as (Double, Double):
        print("an tuple is \(x), \(y)")
    default:
        print("other type")
    }
}
